<?php

class FormView extends CWidget {

	public $model;
	public $title = CONSTANT::OP_SEARCH;
	public $activeForm = array();
	public $fields = array();
	public $useGroup = false;
	public $template;
	
	public function init() {
		parent::init();
		$this->activeForm = array_merge(
			array('htmlOptions'=>array('enctype'=>'multipart/form-data')),
			$this->activeForm
		);
		$this->activeForm = $this->beginWidget('CActiveFormAdv', $this->activeForm);
		
		$fields = array();
		if($this->useGroup) {
			foreach($this->fields as $gName=>$gFields) {
				if(is_string($gFields)) {
					$fields[$gName] = $gFields;
				} else {
					$fields[$gName] = $this->initFields($gFields);
				}
			}	
		} else {
			$fields = $this->initFields($this->fields);
		}
		$this->fields = $fields;
		if(!$this->template) {
			$this->template = dirname(__FILE__).'/assets/form.php';
		} else {
			$this->template = Yii::getPathOfAlias($this->template).'.php';
		}
		ob_start();
	}
	
	
	public function initFields($initFields) {
		$fields = array();
		
		foreach($initFields as $name=>$field) {
			if(is_string($name) && $field === false) continue;
			
			$params = array(
				'attribute' => $name,
				'model' => $this->model,
				'activeForm' => $this->activeForm,
				'owner' => $this,
			);
			
			if(is_string($field)) {
				$name = $field;
				$field = array(
					'class' => 'ActiveFormField',
					'attribute' => $name,
					'params' => array('textField'),
				);
			}
			
			if(!isset($field['class'])) {
				if(isset($field['visible'])) {
					$visible = $field['visible'];
					unset($field['visible']);
				} else {
					$visible = null;
				}
				$field = array(
					'class' => 'ActiveFormField',
					'params' => $field,
					'visible' => $visible,
				);
			} 
			
			$obj = Yii::createComponent(array_merge($params, $field));
			$fields[$name] = $obj;
			
		}
		
		return $fields;
		
	}
	
	public function run() {
		$content = ob_get_clean();
		$this->renderFile($this->template, array(
			'model' => $this->model,
			'form' => $this->activeForm,
		));
		echo $content;
		$this->endWidget();
	}

}

class FormField extends CComponent {
	public $visible = null;
	public $activeForm;
	public $model;
	public $attribute;
	public $owner;
	
	public function render($model = null, $attribute = null) {
		if($this->getIsVisible()) {
			return $this->renderInternal($model?$model:$this->model,
				$attribute?$attribute:$this->attribute
			);
		} else {
			return false;
		}
	}
	
	public function getIsVisible() {
		if($this->visible === null) return true;
		return $this->evaluateExpression($this->visible, array(
			'model'=>$this->model, 
			'attribute'=>$this->attribute
		));
	}
	
	public function renderInternal($model, $attribute) {
	}
}

class ActiveFormField extends FormField {
	
	public $params = array();
		
	public function renderInternal($model, $attribute) {
		$params = $this->params;
		$fieldType = array_shift($params);
		array_unshift($params, $attribute);
		array_unshift($params, $model);
		return call_user_func_array(array($this->activeForm, $fieldType), $params);
	}
}

class WidgetField extends FormField {
	
	public function renderInternal($model, $attribute) {
		ob_start();
		$this->runWidget($model, $attribute);
		$content = ob_get_contents();
		ob_end_clean();
		return $content;
	}
	
	public function runWidget($model, $attribute) {
	}
	
}

class DatetimePickerField extends WidgetField {
	public $params = array();
	public function runWidget($model, $attribute) {
		$this->owner->widget('lib.widgets.my97date.CMy97DatePicker', array_merge($this->params, array(
 			'model' => $model,
 			'attribute' => $attribute,
		)));
	}
	
	
}

class LinkageField extends FormField {
	
	public $name = '';
	public $fields = array();
	
	public function renderInternal($model, $attribute) {
		$linkage = $this->activeForm->linkage($this->name);
		$result = '';
		foreach($this->fields as $one) {
			$defaultParams = array(
				$model,
			);
			$params = is_array($one) ? $one : array($one);
			$params = array_merge($defaultParams, $params);
			$result .= call_user_func_array(array($linkage, 'dropDownList'), $params);
			
		}
		return $result;
	}
	
}


class FileField extends WidgetField {
	public function runWidget($model, $attribute) {
		$this->activeForm->fileField($model, $attribute);
	}
}
class RichtextField extends WidgetField {
	public $params;
	public function runWidget($model, $attribute) {
		$this->owner->widget('lib.widgets.xheditor.CXhEditor', array_merge($this->params, array(
			'model' => $model,
			'attribute' => $attribute,
			'options' => array(
				//'upImgUrl' => Yii::app()->baseUrl.'/xhUpload.php',
				'upImgUrl' => Yii::app()->createUrl('/xhUpload'),
				'upImgExt' => 'jpg,jpeg,gif,png'
			)
		)));
	}
}

class StringField extends FormField {
	public function renderInternal($model, $attribute) {
		return CHtml::value($model, $attribute);
	}
}

class ButtonListField extends WidgetField {
	public $data = array();
	public $params = array();
	public function runWidget($model, $attribute) {
		$this->owner->widget('ButtonList', array_merge($this->params, array(
			'form' => $this->activeForm,
			'model' => $model,
			'attribute' => $attribute,
			'data' => $this->data,
		)));
	}
}

class CitySelect extends WidgetField {
	public $htmlOptions = array();
	public function runWidget($model, $attribute) {
		LyscHtml::citySelector($this->activeForm, $model, $attribute,$this->htmlOptions);
	}
}

?>